function [mainText_new,keyText,numText] = colourAll(fname,parsed,fname_out,new_colour)
% Change the colours of all metabolite nodes to a specific colour.
%
% USAGE:
%
%    [mainText_new, keyText, numText] = colourAll(fname, parsed, fname_out, new_colour)
%
% INPUTS:
%
%    fname:           An XML file that needs to be modified to change the colour
%                     of all metabolite nodes
%    fanme_out:       The name of the output XML file
%    parsed:          A parsed model structure generated by `parseCD` function
%    new_colour:      a colour hex code such as '5bcdb8' or '#5bcdb8'. A list of
%                     reaction IDs that need to be highilighted by
%                     changing the colour attributes of the reaciton links in the
%                     `CellDesigner` model. The first column stores a list of
%                     reaction IDs whose reaction links need to be highlighted,
%                     whereas the second column saves a list of html Colours.
%
% OPTIONAL OUTPUT:
%    mainText_new:    Lines of the XML file
%    keyText:         Lists of retrieved alias and species IDs
%    numText:         Lists of the corresponding line numbers for each alias
%                     and species ID

if nargin<2 || isempty(fname_out)

    [fname_out, fpath]=uiputfile('*.xml','CellDesigner SBML Source File');
    if(fname_out==0)
        return;
    end
    f_out=fopen([fpath,fname_out],'w');
else
    f_out=fopen(fname_out,'w');
end

if nargin<1 || isempty(fname)
    [fname, fpath]=uigetfile('*.xml','CellDesigner SBML Source File');
    if(fname==0)
        return;
    end
    f_id=fopen([fpath,fname],'r');
else
    f_id=fopen(fname,'r');

end




% if isfield(parsed.r_info,'XMLtext');
%     for n=1:length({parsed.r_info.XMLtext(:).str}')
%         MainTxt(n,1)=cellstr(parsed.r_info.XMLtext(n).str);
%     end
%
%
% else
%     errordlg('The XMLtext doesn''t exit in the parsed model structure');
% end



r_info=parsed.r_info;

% list_nodes=list_Rxn;


% There are three different IDs for each reaction



[ID_row,ID_Column]=size(r_info.ID);

for m=1:ID_row;
    for n=1:ID_Column;
        r=iscellstr(r_info.ID(m,n));
        if ~r;
            %results(or,1)=m;
            %results(or,2)=n;
            r_info.ID(m,n)={'unknown'};
            disp(r_info.ID{m,n});
        end;
    end;
end


showprogress(0,'progressing')

num=[];




% num_1=find(ismember(r_info.ID(:,3),list_nodes(:,1)))
% % num_2=find(ismember(r_info.ID(:,2),list_nodes(:,1)))
% % num_3=find(ismember(r_info.ID(:,3),list_nodes(:,1)))
% %
% % [biggest,ind]=max([length(num_1),length(num_2),length(num_3)])
% %
% % switch ind
% %     case 1
% %         num=num_1;
% %     case 2
% %         num=num_2;
% %     case 3
% %         num=num_3;
% % end
% num=num_1;

% % for i=1:length(list_nodes);
% %
% %             %  waitbar(((i/length(list_nodes))*1/4)/4,h);
% %
% %     %if ~isempty(list_nodes{i,2})
% %         re=find(~cellfun('isempty',strfind(r_info.ID(:,3),list_nodes{i,1})))
% %         if isempty(re)
% %             re=find(~cellfun('isempty',strfind(r_info.ID(:,2),list_nodes{i,1})))
% %             if isempty(re)
% %                 re=find(~cellfun('isempty',strfind(r_info.ID(:,1),list_nodes{i,1})))
% %
% %                 if isempty(re)
% %                     errordlg('The provided reaction cannot be found in the parsed model structure');
% %                 else
% %                     num(i)=re(1);
% %                 end
% %             else
% %                 num(i)=re(1);
% %             end
% %         else
% %             num(i)=re(1);  % obtain the number of reaction whose corresponding nodes on the layout will be highlighted.
% %         end
% %    % end
% %
% % end



% if the provided reaction IDs cannot be found in the third column of the
% reaction list, then the following codes search the column 2 intead for reaction IDs.


% if isempty(num)
%     for i=1:length(list_nodes);
%         num(i)=find(~cellfun('isempty',strfind(r_info.ID(:,2),list_nodes{i,1})))
%     end
% end

% wiP=size(r_info.baseReactant,2)
% he=length(num);
% baseR=cell(he,wiR);
% baseP=cell(he,wiP);

baseC_R={};
baseC_P={};



% % for i=1:length(num)  % "num" contains number of reaction nodes
% %     if ismember(i, progress)~=0||i==total_length;
% %         waitbar((i/total_length*2/4)/4,h);
% %     end
% %
% %
% %    baseR.(list_nodes{i})=r_info.baseReactant(num(i),:)
% %
% %     % add other reactants
% %     len_r_st=length(baseR.(list_nodes{i}))
% %     len_r_ed=length(r_info.reactant(num(i),:))
% %     baseR.(list_nodes{i})(len_r_st+1:len_r_st+len_r_ed)=r_info.reactant(num(i),:);
% %
% %     % include all the baseProducts
% %     baseP.(list_nodes{i})=r_info.baseProduct(num(i),:)
% %
% %     % add other products.
% %     len_r_st=length(baseP.(list_nodes{i}))
% %     len_r_ed=length(r_info.product(num(i),:))
% %     baseP.(list_nodes{i})(len_r_st+1:len_r_st+len_r_ed)=r_info.product(num(i),:);
% %
% %     %% by default
% %     if size(list_nodes,2)<2||isempty(list_nodes{i,2});
% %         list_nodes(i,2)={'#FFCDE2BD'}; % the default color is set.
% %     end
% %
% %
% %     %% highlight reactions
% %
% %
% %     for c=1:size(baseR.(list_nodes{i}),2);
% %         if ~isempty(baseR.(list_nodes{i}){c})
% %
% %             baseC_R.(list_nodes{i})(c)=strrep(list_nodes(i,2),'#',''); % remove the '#' sign from the hex codes.
% %         end
% %     end
% %
% %
% %     for c=1:size(baseP.(list_nodes{i}),2);
% %         if ~isempty(baseP.(list_nodes{i}){c})
% %             baseC_P.(list_nodes{i})(c)=strrep(list_nodes(i,2),'#',''); % remove the '#' sign from the hex codes.
% %         end
% %     end
% %
% % if metMode~=0;  % check if the mets are needed to be highlighted
% %     numMet=size(list_Met,1)
% %
% %     if i<=numMet  % the number of lines of metaoblites may be less than that of reactions
% %         for t=1:size(list_Met(i,:),2) % number of metabolites
% %             if ~isempty(list_Met{i,t})
% %                 met(i,t)=retrieveMet(parsed,list_Met(i,t))
% %                 list_M_species(i,2*t-1)={met(i,t).speciesAlliens}
% %
% %             end
% %         end
% %
% %     end
% % end
% %
% %
% %    for r=2:2:length(baseR.(list_nodes{i}))
% %
% %         list_R(i,r/2)=baseR.(list_nodes{i})(r);
% %     end
% %     for d=1:2:length(baseR.(list_nodes{i}))-1
% %         list_R_M(i,(d+1)/2)=baseR.(list_nodes{i})(d)
% %     end
% %
% %     for r=2:2:length(baseP.(list_nodes{i}))
% %
% %         list_P(i,r/2)=baseP.(list_nodes{i})(r);
% %     end
% %
% %
% %     for d=1:2:length(baseP.(list_nodes{i}))-1
% %         list_P_M(i,(d+1)/2)=baseP.(list_nodes{i})(d)
% %     end
% %
% %
% %
% %     for r=2:2:length(baseC_R.(list_nodes{i}))
% %         list_C_R(i,r/2)=baseC_R.(list_nodes{i})(r); % each reaction has the same color
% %     end
% %
% %     for r=2:2:length(baseC_P.(list_nodes{i}))
% %         list_C_P(i,r/2)=baseC_P.(list_nodes{i})(r); % each reaction has the same color
% %     end
% %
% %
% % end



% extract list of pairs of metabolite identifies

% each pair contains a metabolite ID and a alias

%
% for i=1:length(num)
%
%
%
%
%
% end


% numOfLine=0;
% MainTxt={};

% while ~feof(f_id);
%
%     numOfLine=numOfLine+1;
%     rem=fgets(f_id);
%     %     try
%     MainTxt(numOfLine,1)=cellstr(rem);
%
%     %     catch
%     %         disp(rem);
%     %     end
%
% end


toFD=[];
toFD.str=[];

% celldesigner nodes


toFD(1).str='<celldesigner:speciesAlias';   % starting line

listID={'id',' species'};

toFD(2).str='<celldesigner:paint';  % ending line

toFD(3).str='</celldesigner:listOfSpeciesAliases>';

listColour={'color','scheme'}

sectionKey(1).str='<celldesigner:listOfSpeciesAliases>';

sectionKey(2).str='</celldesigner:listOfSpeciesAliases>';

% sectionKey(1).str='<celldesigner:extension>';
% sectionKey(2).str='</celldesigner:extension>';


new=0;

secKey=0;


a=0;
found=0;



showprogress(1/4);


numL=0;
first=0;
second=0;



% total_length=length(MainTxt);
% progress=zeros(10,1);
% for ii=1:10;
%
% progress(ii,1)=ii*floor(total_length/10)



t=0;

while ~feof(f_id);

    t=t+1;
    rem=fgets(f_id);
    %     try
    MainTxt(t,1)=cellstr(rem);

    %     catch
    %         disp(rem);
    %     end

    % end
    %
    % for t=1:length(MainTxt);

    %     if ismember(t, progress)~=0||t==total_length;
    %         waitbar((t/total_length*2/4+1)/4,h);
    %     end

    new=new+1;

    MainTxt_new(new,1)=MainTxt(t);
    section_1=strfind(MainTxt(t),sectionKey(1).str);
    section_2=strfind(MainTxt(t),sectionKey(2).str);


    if (~isempty(section_1{1,1}))
        secKey=1;
        fprintf('found the metKeyword: %s',MainTxt{t});
    elseif (~isempty(section_2{1,1}))
        secKey=0;
        fprintf('Cannot found the metKeyword:  %s',MainTxt{t});
    end


    if secKey==0
        continue;
    end





    result=strfind(MainTxt(t),toFD(1).str)

    if ~isempty(result{1,1})&&(secKey==1)

        numL=numL+1;

        [st,ed]=position(MainTxt{t},listID{1})

        str=MainTxt{t}



        keyText(numL,1)={str(st:ed)};


        [st,ed]=position(MainTxt{t},listID{2})

        str=MainTxt{t}

        keyText(numL,2)={str(st:ed)};





        first=first+1;


        numText(numL,first)=t;
        first=0;
        second=1;

    end

    res=strfind(MainTxt(t),toFD(2).str)

    if ~isempty(res{1,1})&&(secKey==1)
        second=second+1;

        numText(numL,second)=t;

    end

    %for c=1:length(listColour);
    %         if strcmp(listColour(1),'color')
    %             [st,ed]=position(MainTxt_new{new},listColour{1})
    %             str_colour=MainTxt_new{new};
    %             str_colour(st:ed)=char(new_colour)
    %             MainTxt_new{new}=str_colour;
    %
    %         end
    %end

    res_end=strfind(MainTxt(t),toFD(3).str);
    if ~isempty(res_end{1,1})
        found=0;
        break;
    end


end

%% old version of the "write" funciton

% for d=1:length(MainTxt_new);
%
%     fprintf(f_out,'%s\n',char(MainTxt_new(d)));
% end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%



fclose(f_out);
mainText_new=MainTxt_new;

% for d=1:length(MainTxt_new)
%     parsed.r_info.XMLtext(d).str=MainTxt_new(d,1)
% end
% parsed_updated=parsed;


% writeCD(parsed_updated,fname_out)


parsed_test=parsed;
str_colour='<celldesigner:paint color="fff5f8fa" scheme="Color"/>';
[st,ed]=position(str_colour,listColour{1})


new_colour=strrep(new_colour,'#',''); % remove the '#' sign from the hex codes.
if length(new_colour)<8;

    new_colour=['FF',new_colour];  % add the compatibility with six-digit code
end


str_colour(st:ed)=char(new_colour)

for d=1:length(numText);
    parsed_test.r_info.XMLtext(numText(d,2)).str=str_colour;
end

writeCD(parsed_test,fname_out)


showprogress(4/4);

end



function [p_st,p_ed]=position(str_long,str_ID)
%% name='metaid';

ind_pos=strfind(str_long,str_ID);

l=length(str_ID)+2;
try
    p_st=ind_pos(1)+l
catch
    error('cannot find the Keyword in the the line')
end

end_rem=strfind(str_long(p_st:end),'"');

p_ed=end_rem(1)+p_st-2;


end
